package com.personnalManagerSystem.controller;



import com.personnalManagerSystem.model.Department;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liqing
 * @since 2022-03-27
 */
@Controller
@RequestMapping("/department")
public class DepartmentController  extends BaseController{
    @RequestMapping("/list")
    public String list(Model model,
                       String option,
                       String searchContent,
                       Integer pageNum,
                       Integer pageSize){
        baseList(model,option,searchContent,pageNum,pageSize,iDepartmentService);
        List<Department> departmentList = iDepartmentService.list();
        model.addAttribute("departmentList",departmentList);
        return "view/department";
    }
    @RequestMapping("/checkDepartmentName")
    @ResponseBody
    /**
     * @Description  在添加一个部门信息之前先对部门名进行重复性校验
     * @author liqing
     * @data 2022/4/2  14:32
     * @param departmentName
     * @return java.util.Map<java.lang.String,java.lang.Object>
     */
    public Map<String,Object> checkDepartmentName(@RequestParam("departmentName")String departmentName){
        System.out.println("departmentName:"+departmentName);
        Map<String,Object> map = new HashMap<>();
        Department department = iDepartmentService.checkDepartmentName(departmentName);
        System.out.println("department:"+department);
        if(department==null){
            map.put("code",200);
            map.put("msg","该部门名不存在");
        }else{
            map.put("code",201);
            map.put("msg","该部门名已经存在,请重新输入一个");
        }
        return map;
    }
    @RequestMapping("/add")
    public String add(Department department){
        //System.out.println("department:"+department);
        iDepartmentService.save(department);
        return "redirect:/department/list";
    }
    @RequestMapping("/del")
    public String del(@RequestParam("departmentId") Integer departmentId){
        //System.out.println("departmentId:"+departmentId);
        iDepartmentService.removeById(departmentId);
        return "redirect:/department/list";
    }
    @RequestMapping("/edit")
    public String edit(Department department){
        //System.out.println("department:"+department);
        iDepartmentService.saveOrUpdate(department);
        return "redirect:/department/list";
    }


}

